package com.sright.bcard.content.biz.manager.impl;

import com.sright.bcard.common.service.share.enums.TemplateTypeEnum;
import com.sright.bcard.common.service.share.utils.DataConvertUtils;
import com.sright.bcard.content.biz.manager.TemplateManager;
import com.sright.bcard.content.dal.dao.CardBaseInfoDAO;
import com.sright.bcard.content.dal.dao.CardDAO;
import com.sright.bcard.content.dal.dao.TemplateDAO;
import com.sright.bcard.content.dal.entity.CardBaseInfoDO;
import com.sright.bcard.content.dal.entity.CardDO;
import com.sright.bcard.content.dal.entity.TemplateDO;
import com.sright.bcard.common.service.share.dto.content.CardTemplateDTO;
import com.sright.bcard.user.share.request.TokenParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class TemplateManagerImpl implements TemplateManager {
    @Autowired
    private CardDAO cardDAO;

    @Autowired
    private CardBaseInfoDAO cardBaseInfoDAO;

    @Autowired
    private TemplateDAO templateDAO;

    @Override
    public List<TemplateDO> templateList(TokenParam tokenParam) {
        // 通过memberId查询user
        CardDO cardDO = cardDAO.selectByUserId(tokenParam.getPersonalUserId());
        CardBaseInfoDO cardBaseInfoDO = cardBaseInfoDAO.selectById(cardDO.getBaseInfoId());

        List<TemplateDO> templateDOList = templateDAO.selectByType(TemplateTypeEnum.PERSONAL.getValue());
        if (templateDOList.size() > 0) {
            for (TemplateDO templateDO : templateDOList) {
                if (templateDO.getId().equals(cardBaseInfoDO.getTemplateId())) {
                    templateDO.setInUse(1);
                } else {
                    templateDO.setInUse(0);
                }
            }
        }
        return templateDOList;
    }

    @Override
    public Boolean selectTemplate(Long cardId, Long templateId) {
        CardDO cardDO = cardDAO.selectById(cardId);
        CardBaseInfoDO cardBaseInfoDO = cardBaseInfoDAO.selectById(cardDO.getBaseInfoId());
        cardBaseInfoDO.setTemplateId(templateId);
        return cardBaseInfoDAO.updateSelective(cardBaseInfoDO);
    }

    @Override
    public List<CardTemplateDTO> allTemplateList() {
        List<TemplateDO> templateDOS = templateDAO.allTemplateList();
        // DO 转换 DTO
        List<CardTemplateDTO> cardTemplateDTOList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(templateDOS)) {
            DataConvertUtils.convertDOList2DTOList(templateDOS, cardTemplateDTOList, CardTemplateDTO.class);
        }
        return cardTemplateDTOList;
    }

    @Override
    public List<CardTemplateDTO> templates() {
        List<TemplateDO> templateDOList = templateDAO.templates();
        // DO 转换 DTO
        List<CardTemplateDTO> cardTemplateDTOS = new ArrayList<>();
        DataConvertUtils.convertDOList2DTOList(templateDOList, cardTemplateDTOS, CardTemplateDTO.class);
        return cardTemplateDTOS;
    }
}
